import { message } from "antd";
import axios from "axios";
const request = axios.create({
    baseURL: 'http://blogapi.shbwyz.com',
    timeout: 1000,
});
let count = 0
let Login = null
// 添加请求拦截器
request.interceptors.request.use(function (config) {
    count = count + 1
    if (count === 0) {
        Login = message.loading("加载中...", 0)
    }
    // 在发送请求之前做些什么
    //存token
    let token = localStorage.getItem("token")
    if (token) {
        config.headers.Authorization = `Bearer ${token}`
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
request.interceptors.response.use(function (response) {
    count = count - 1
    if (count === 0) {
        if (Login) {
            Login()
        }
    }
    // 对响应数据做点什么
    return response;
}, function (error) {
    count = count - 1
    if (count === 0) {
        if (Login) {
            Login()
        }
    }
    // 对响应错误做点什么
    switch (error.response.status) {
        case 400:
            console.log("获取值有误")
            break;
        case 500:
            console.log("服务器错误")
            break;
    }
    return Promise.reject(error);
});
export default request